package com.mine.frame.common.base

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.ToastUtils
import com.mine.frame.common.bus.RxBus
import com.mine.frame.common.bus.RxBusBean
import com.mine.frame.common.utils.DisposableUtils
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.disposables.Disposable

/**
 * company 广东玛格家居有限公司
 * @author smoker
 * @Date 2021/11/12
 * Description:
 */
abstract class BaseFragment<IV : IBaseView, P : BasePresenter<IV>> : Fragment(), IBaseView {

    private lateinit var mLayout: View

    /**
     * 当前presenter
     */
    protected lateinit var mPresenter: P

    /**eventBus*/
    private lateinit var busDisposable: Disposable

    /**
     * 当前界面的布局文件
     */
    @LayoutRes
    abstract fun initLayoutId(): Int


    /**
     * 初始化Presenter
     */
    abstract fun initPresenter(): P

    /**
     * 注册eventBus
     * 复写：true 注册事件
     *      false 不注册事件
     */
    protected open fun openRxBus() = false

    /**
     * 事件总线回调
     */
    protected open fun onReceiveEvent(bus: RxBusBean) {}

    /**
     * 页面初始化之后执行的方法
     */
    protected abstract fun initLayoutAfter(savedInstanceState: Bundle?)


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mLayout = inflater.inflate(initLayoutId(), container, false)
        return mLayout
    }


    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        mPresenter = initPresenter()
        initLayoutAfter(savedInstanceState)

    }

    override fun onStart() {
        super.onStart()
        if (openRxBus()) {
            registerBus()
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        unregisterBus()
    }

    override fun onDestroy() {
        super.onDestroy()
        DisposableUtils.instance.remove(getDisposableTag())
    }

    /**
     * 注册eventBus
     */
    private fun registerBus() {
        busDisposable = RxBus.instance.toFlowable()
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe { bus ->
                onReceiveEvent(bus)
            }
    }

    /**
     * 注销eventBus
     */
    private fun unregisterBus() {
        if (this::busDisposable.isInitialized && !this.busDisposable.isDisposed) {
            this.busDisposable.dispose()
        }
    }

    override fun getDisposableTag() = javaClass.name

    override fun getContext(): Context = context

    override fun showInfo(msg: String) {
        ToastUtils.showShort(msg)
    }

    override fun showErr(msg: String) {
        ToastUtils.showShort(msg)
    }

    override fun showSuccess(str: String) {
        ToastUtils.showShort(str)
    }

    override fun showWrong(wrong: String) {
        ToastUtils.showShort(wrong)
    }

    override fun showToast(msg: String) {
        ToastUtils.showShort(msg)
    }

    override fun showProgress(msg: String) {
        ToastUtils.showShort(msg)
    }

    override fun showProgressWithoutBg(msg: String) {
        ToastUtils.showShort(msg)
    }

    override fun hintProgress() {

    }

}